//
//  TSSendMsgListViewController.m
//  chart
//
//  Created by xu on 15/7/29.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSSendMsgListViewController.h"
#import "TSSendMsgTableView.h"
#import "TSCreateMsgViewController.h"
#import "TSRecycleMsgListViewController.h"
@implementation TSSendMsgListViewController{
    TSSendMsgTableView *tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageChangeOrPostSucceeded:) name:TSNotification_MessageChangeSucceeded object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageChangeOrPostSucceeded:) name:TSNotification_MessagePostSucceeded object:nil];
    
    self.navigationItem.title = @"发送列表";
    
    WS(weakSelf);
    [self rightBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"topic_follow_add"] barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        UIActionSheet *action_sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:weakSelf cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"创建消息",@"回收站", nil];
        [action_sheet showInView:weakSelf.view];
        
    }];
    
    tableView = [[TSSendMsgTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain IsAudit:NO];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableView];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self createMsg];
    }else if (buttonIndex == 1) {
        [self showRecycle];
    }
}

-(void)showRecycle
{
    TSRecycleMsgListViewController *msg_vc = [[TSRecycleMsgListViewController alloc] init];
    TSBaseNavigationController *nav_vc = [[TSBaseNavigationController alloc] initWithRootViewController:msg_vc];
    [self presentViewController:nav_vc animated:YES completion:nil];
}

/* 创建新信息 */
- (void)createMsg
{
    TSCreateMsgViewController *msg_vc = [[TSCreateMsgViewController alloc] init];
    TSBaseNavigationController *nav_vc = [[TSBaseNavigationController alloc] initWithRootViewController:msg_vc];
    [self presentViewController:nav_vc animated:YES completion:nil];
}

/* 修改信息后更新发送列表 */
- (void)messageChangeOrPostSucceeded:(NSNotification *)noti
{
    if (tableView) {
        [tableView.header beginRefreshing];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
